package cn.wolfcode.crm.web.controller;

import cn.wolfcode.crm.domain.Member;
import cn.wolfcode.crm.query.MemberQueryObject;
import cn.wolfcode.crm.serivce.IMemberService;
import cn.wolfcode.crm.util.JSONResult;
import cn.wolfcode.crm.util.PageResult;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("member")
public class MemberController {
    @Autowired
    private IMemberService service;

    @RequestMapping("view")
    @RequiresPermissions(value={"member:view","会员界面"},logical = Logical.OR)
    public String view() throws Exception {
        return "member";
    }
    @RequestMapping("selectByPrimaryKey")
    @ResponseBody
    public Member selectByPrimaryKey(Long id) throws Exception {
        Member member = service.selectByPrimaryKey(id);
        return member;
    }

    @RequestMapping("selectByMemberClientId")
    @ResponseBody
    public Member selectByMemberClientId(Long id) throws Exception {
        Member member = service.selectByMemberClientId(id);
        return member;
    }

    @RequestMapping("selectByRechargeTimeId")
    @ResponseBody
    public Member selectByRechargeTimeId(Long id) throws Exception {
        Member member = service.selectByRechargeTimeId(id);
        return member;
    }

    @RequestMapping("selectBirthday")
    @ResponseBody
    public Object selectBirthday() throws Exception {
        Integer number = service.selectBirthday();
        return number;
    }
    @RequestMapping("selectBirthdayByMonth")
    @ResponseBody
    public Object selectBirthdayByMonth() throws Exception {
        Integer number = service.selectBirthdayByMonth();
        return number;
    }

    @RequestMapping("selectCount")
    @ResponseBody
    public Object selectCount() throws Exception {
        Integer number = service.selectCount();
        return number;
    }

    @RequestMapping("query")
    @ResponseBody
    public PageResult query(MemberQueryObject qo) throws Exception {
        PageResult query = service.query(qo);
        return query;
    }
    @RequestMapping("selectAllByShopName")
    @ResponseBody
    public PageResult selectAllByShopName(MemberQueryObject qo) throws Exception {
        PageResult query = service.selectAllByShopName(qo);
        return query;
    }
    @RequestMapping("selectAll")
    @ResponseBody
    public List<Member> selectAll() throws Exception {
        List<Member> members = service.selectAll();
        return members;
    }
    @RequestMapping("selectByChangeKey")
    @ResponseBody
    public List<Member> selectByChangeKey() throws Exception {
        List<Member> members = service.selectByChangeKey();         //有问题, 没有实现方法
        return members;
    }

    //删除会员
    @RequestMapping("delete")
    @ResponseBody
    public JSONResult delete(Long id) throws Exception {
        try {
            service.deleteByPrimaryKey(id);
            return new JSONResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new JSONResult().mark("操作失败!");
        }
    }
    @RequestMapping("zeroPoint")
    @ResponseBody
    public JSONResult zeroPoint(Member member) throws Exception {
        try {
            service.zeroPoint(member);
            return new JSONResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new JSONResult().mark("操作失败!");
        }
    }
    //验证用户名的方法
    @RequestMapping("validate")
    @ResponseBody
    public Boolean validate(String cardNumber,Long id) throws Exception {
        Boolean aBoolean = service.selectByCarNumber(cardNumber,id);
        return aBoolean;
    }

    @RequestMapping("saveOrUpdate")
    @ResponseBody
    @RequiresPermissions(value={"member:saveOrUpdate","会员新增/编辑"},logical = Logical.OR)
    public JSONResult query(Member emp) throws Exception {
        try {
            service.saveOrUpdate(emp);
            return new JSONResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new JSONResult().mark("保存失败!");
        }
    }

    //下载
    @RequestMapping("exportXls")
    public void exportXls(HttpServletResponse response) throws Exception {
        //设置下载响应头
        response.setHeader("Content-Disposition","attachment;filename=member.xls");
        //创建excel文件
        Workbook wb = new HSSFWorkbook();
        //创建工作薄
        Sheet sheet = wb.createSheet("member");
        //查询会员数据
        List<Member> members = service.selectAll();
        //创建表格第一行
        Row row = sheet.createRow(0);
        //设置标题
        row.createCell(0).setCellValue("卡号");
        row.createCell(1).setCellValue("会员姓名");
        row.createCell(2).setCellValue("电话");
        row.createCell(3).setCellValue("等级");
        row.createCell(4).setCellValue("积分");
        row.createCell(5).setCellValue("余额");
        row.createCell(6).setCellValue("地址");
        row.createCell(7).setCellValue("客户来源");
        //设置第二行开始
        for(int i=0;i< members.size();i++){
            //从第二行开始创建行
            Row row1 = sheet.createRow(i + 1);
            //从列表的第一开始取出所有的会员对象
            Member member = members.get(i);
            //创建单元格
            row1.createCell(0).setCellValue(member.getCardNumber());
            row1.createCell(1).setCellValue(member.getName());
            row1.createCell(2).setCellValue(member.getTel());
            row1.createCell(3).setCellValue(member.getLevelName());
            row1.createCell(4).setCellValue(member.getPoints());
            row1.createCell(5).setCellValue(member.getBalance().toString());
            row1.createCell(6).setCellValue(member.getAddress());
            row1.createCell(7).setCellValue(member.getSourceName());
        }
        //输出到浏览器
        wb.write(response.getOutputStream());
    }

    //上传
    @RequestMapping("importXls")
    public void importXls(MultipartFile file) throws Exception {
        //获取excel文件
        Workbook wb = new HSSFWorkbook(file.getInputStream());
        //获取工作簿
        Sheet sheet = wb.getSheet("member");
        //获取总行数
        int lastRowNum = sheet.getLastRowNum();
        for(int i=1;i<=lastRowNum;i++){
            Row row = sheet.getRow(i);
            Member member = new Member();
            member.setCardNumber(row.getCell(0).getStringCellValue());
            member.setName(row.getCell(1).getStringCellValue());
            member.setTel(row.getCell(2).getStringCellValue());
            member.setLevelName(row.getCell(3).getStringCellValue());
            member.setPoints(Integer.parseInt(row.getCell(4).getStringCellValue()));
            member.setBalance(new BigDecimal(row.getCell(5).getStringCellValue()));
            member.setAddress(row.getCell(3).getStringCellValue());
            member.setSourceName(row.getCell(3).getStringCellValue());
            //保存对象
            service.saveOrUpdate(member);
        }
    }
    @RequestMapping("changeType")
    @ResponseBody
    public JSONResult changeType(Long id) throws Exception {
        try {
            service.changeType(id);
            return new JSONResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new JSONResult().mark("操作失败!");
        }
    }

    @RequestMapping("orderByAmount")
    @ResponseBody
    public List<Map<String, Object>> orderByAmount() throws Exception {
        return service.orderByAmount();
    }

    @RequestMapping("selectByTotalAmount")
    @ResponseBody
    public Object selectByTotalAmount(String cardNumber) throws Exception {
        return service.selectByTotalAmount(cardNumber);
    }
//    public List<Account> selectByTotalAmount(String cardNumber) throws Exception {
//        return service.selectByTotalAmount(cardNumber);
//    }
}
